﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Data.SqlClient;
//using Player.MyAttribute;
//using Player.StoreProcedure;

//namespace Player.Gataway
//{
//    public class InstallationPermissionList :  GatewayStoredProcedure<
//             InstallationPermissionList,
//             InstallationPermissionList.Input,
//             InstallationPermissionList.Output,
//             InstallationPermissionList.Variables>
//    {
//        #region Mapping definition

//        public enum Variables : int
//        {
//        }

//        public struct Input
//        {
//        }

//        public struct Output
//        {
//            [ColumnMapping(typeof(InstallationPermission), (int)InstallationPermission.TrusteeSid)]
//            public byte[] TrusteeSid;

//            [ColumnMapping(typeof(InstallationPermission), (int)InstallationPermission.TrusteeType)]
//            public int TrusteeType;

//            [ColumnMapping(typeof(InstallationPermission), (int)InstallationPermission.AccessRight)]
//            public int AccessRight;
//        }
//        #endregion

//        [StoredProcedure(ExecuteAs = @"'NT Authority\System'", ExecuteBy = "user")]
//        public static void StoredProcedure()
//        {
//            string sql = Build("SELECT", AllColumns, "FROM", typeof(InstallationPermission));
//            using (SqlCommand command = CreateCommand(sql))
//            {
//                SqlContext.Pipe.ExecuteAndSend(command);
//            }
//        }
//    }

//    public class InstallationPermissionAdd : GatewayStoredProcedure<
//         InstallationPermissionAdd,
//         InstallationPermissionAdd.Input,
//         InstallationPermissionAdd.Output,
//         InstallationPermissionAdd.Variables>
//    {
//        #region Mapping definition

//        public enum Variables : int
//        {
//            [Variable(Direction.Input)]
//            TrusteeSid,
//            [Variable(Direction.Input)]
//            TrusteeType,
//            [Variable(Direction.Input)]
//            AccessRight,
//            [Variable(Direction.Output)]
//            Count
//        }

//        public struct Input
//        {
//            [InputMapping((int)Variables.TrusteeSid)]
//            public byte[] TrusteeSid;

//            [InputMapping((int)Variables.TrusteeType)]
//            public int TrusteeType;

//            [InputMapping((int)Variables.AccessRight)]
//            public int AccessRight;

//            public Input(byte[] trusteeSid, int trusteeType, int accessRight)
//            {
//                TrusteeSid = trusteeSid;
//                TrusteeType = trusteeType;
//                AccessRight = accessRight;
//            }
//        }

//        public struct Output
//        {
//            [OutputMapping((int)Variables.Count, SqlDbType.Int)]
//            public int Count;
//        }
//        #endregion

//        [StoredProcedure(ExecuteAs = @"'NT Authority\System'", ExecuteBy = "user")]
//        public static void StoredProcedure(byte[] trusteeSid, int trusteeType, int accessRight, out int count)
//        {
//            if (!CanChangeInstallationPermission)
//            {
//                RaiseUnauthorizedException("You have no permission to execute InstallationPermissionAdd stored procedure.");
//            }

//            string sql = Build("INSERT INTO", typeof(InstallationPermission), "(",
//                    List(InstallationPermission.TrusteeSid, InstallationPermission.TrusteeType,
//                    InstallationPermission.AccessRight),
//                    ") VALUES (",
//                    List(Variables.TrusteeSid, Variables.TrusteeType, Variables.AccessRight), ")",
//                    "SET", Variables.Count, "=@@ROWCOUNT");
//            using (SqlCommand command = CreateCommand(sql))
//            {
//                command.Parameters.AddRange(Parameters(new Input(trusteeSid, trusteeType, accessRight)));
//                SqlContext.Pipe.ExecuteAndSend(command);
//                count = 0;
//                command.GetValue(Variables.Count, ref count);
//            }
//        }
//    }

//    public class InstallationPermissionRemove : GatewayStoredProcedure<
//        InstallationPermissionRemove,
//        InstallationPermissionRemove.Input,
//        InstallationPermissionRemove.Output,
//        InstallationPermissionRemove.Variables>
//    {
//        #region Mapping definition

//        public enum Variables : int
//        {
//            [Variable(Direction.Input)]
//            TrusteeSid,
//            [Variable(Direction.Output)]
//            Count
//        }

//        public struct Input
//        {
//            [InputMapping((int)Variables.TrusteeSid)]
//            public byte[] TrusteeSid;

//            public Input(byte[] trusteeSid)
//            {
//                TrusteeSid = trusteeSid;
//            }
//        }

//        public struct Output
//        {
//            [OutputMapping((int)Variables.Count, SqlDbType.Int)]
//            public int Count;
//        }
//        #endregion

//        [StoredProcedure(ExecuteAs = @"'NT Authority\System'", ExecuteBy = "user")]
//        public static void StoredProcedure(byte[] trusteeSid, out int count)
//        {
//            if (!CanChangeInstallationPermission)
//            {
//                RaiseUnauthorizedException("You have no permission to execute InstallationPermissionRemove stored procedure.");
//            }

//            count = 0;
//            string sql = Build("DELETE FROM", typeof(InstallationPermission),
//                 "WHERE", InstallationPermission.TrusteeSid, "=", Variables.TrusteeSid,
//                 "SET", Variables.Count, "=@@ROWCOUNT");
//            using (SqlCommand command = CreateCommand(sql))
//            {
//                command.Parameters.AddRange(Parameters(new Input(trusteeSid)));
//                SqlContext.Pipe.ExecuteAndSend(command);
//                command.GetValue(Variables.Count, ref count);
//            }
//        }
//    }

//    public class InstallationPermissionSet : GatewayStoredProcedure<
//        InstallationPermissionSet,
//        InstallationPermissionSet.Input,
//        InstallationPermissionSet.Output,
//        InstallationPermissionSet.Variables>
//    {
//        #region Mapping definition

//        public enum Variables : int
//        {
//            [Variable(Direction.Input)]
//            AceBuffer,
//            [Variable(Direction.Output)]
//            Count
//        }

//        public struct Input
//        {
//            [InputMapping((int)Variables.AceBuffer)]
//            public byte[] AceBuffer;

//            public Input(byte[] aceBuffer)
//            {
//                AceBuffer = aceBuffer;
//            }
//        }
//        public struct Output
//        {
//            [OutputMapping((int)Variables.Count, SqlDbType.Int)]
//            public int Count;
//        }
//        #endregion

//        [StoredProcedure(ExecuteAs = @"'NT Authority\System'", ExecuteBy = "user")]
//        public static void StoredProcedure(byte[] aceBuffer, out int count)
//        {
//            if (!CanChangeInstallationPermission)
//            {
//                RaiseUnauthorizedException("You have no permission to execute InstallationPermissionSet stored procedure.");
//            }

//            count = 0;
//            AceEntry[] aces = (AceEntry[])DeserializeBuffer(aceBuffer);

//            // Get removed user list
//            List<byte[]> existingTrusteeSids = new List<byte[]>();
//            Permission.GetExistingTrusteeSids(
//                InstallationPermissionList.Execute(SqlHelper.CONTEXT_CONNECTION, new InstallationPermissionList.Input()),
//                o => existingTrusteeSids.Add(o.TrusteeSid));
//            IList<byte[]> removedTrusteeSids = Permission.GetRemovedTrusteeSids(aces, existingTrusteeSids);

//            // Add SQL login and user
//            UserAccountsAdd.Process(aces);

//            StringBuilder sb = new StringBuilder();
//            sb.AppendLine(ToSql("SET", Variables.Count, "= 0"));
//            sb.AppendLine("BEGIN TRANSACTION");
//            sb.AppendLine("BEGIN TRY");

//            // delete ace
//            sb.AppendLine(ToSql("DELETE FROM", typeof(InstallationPermission)));

//            // insert ace
//            foreach (AceEntry ace in aces)
//            {
//                sb.AppendLine(ToSql("INSERT INTO", typeof(InstallationPermission), "(",
//                                    List(InstallationPermission.TrusteeSid,
//                                    InstallationPermission.TrusteeType, InstallationPermission.AccessRight),
//                                    ") VALUES (",
//                                    List(ace.TrusteeSid, ace.TrusteeType.ToString(), ace.AccessRight.ToString()), ")",
//                                    "SET", Variables.Count, "=", Variables.Count, "+@@ROWCOUNT"));
//            }
//            sb.AppendLine("COMMIT TRANSACTION");
//            sb.AppendLine("END TRY");
//            sb.AppendLine("BEGIN CATCH");
//            sb.AppendLine("ROLLBACK TRANSACTION");
//            sb.AppendLine(ToSql("SET", Variables.Count, "= 0"));
//            // rethrow the error
//            sb.AppendLine("EXEC RethrowError");
//            sb.AppendLine("END CATCH");

//            string sql = sb.ToString();
//            using (SqlCommand command = CreateCommand(sql))
//            {
//                command.Parameters.AddRange(Parameters(new Input(aceBuffer)));
//                SqlContext.Pipe.ExecuteAndSend(command);
//                command.GetValue(Variables.Count, ref count);
//            }

//            // Remove user account if needed
//            Permission.RemoveUserAccounts(removedTrusteeSids);
//        }
//    }
//}
